package com.kfyang.manualgear.daoplugin.linemarker;

import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider;
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.psi.*;
import com.intellij.psi.search.PsiShortNamesCache;
import com.kfyang.manualgear.daoplugin.Constants;
import com.kfyang.manualgear.daoplugin.NavigationIcon;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.psi.*;

import java.util.Collection;

public class DaoToYamlLineMarkerProvider extends RelatedItemLineMarkerProvider {

    @Override
    protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo<?>> result) {
        // 跳转类
        if (element instanceof PsiClass interfaceClass && interfaceClass.isInterface()) {
            PsiFile[] files = PsiShortNamesCache.getInstance(element.getProject()).getFilesByName(interfaceClass.getName() + ".yml");
            if (files.length != 1) {
                return;
            }
            YAMLFile yamlFile = (YAMLFile) files[0];

            NavigationGutterIconBuilder<PsiElement> classBuilder = NavigationGutterIconBuilder.create(NavigationIcon.NAV_ICON)
                .setAlignment(GutterIconRenderer.Alignment.CENTER)
                .setTarget(yamlFile)
                .setTooltipTitle("Navigation to Target In Mapper Yaml");
            result.add(classBuilder.createLineMarkerInfo(interfaceClass.getNameIdentifier()));
            return;
        }

        // 跳转方法
        if (!(element instanceof PsiMethod psiMethod)) {
            return;
        }
        PsiElement parent = psiMethod.getParent();
        if (!(parent instanceof PsiClass psiClass && psiClass.isInterface() &&
            psiClass.getAnnotation(Constants.DAO_ANNO) != null)) {
            return;
        }
        PsiFile[] files = PsiShortNamesCache.getInstance(element.getProject()).getFilesByName(psiClass.getName() + ".yml");
        if (files.length != 1) {
            return;
        }
        YAMLFile yamlFile = (YAMLFile) files[0];


        // 找到对应位置
        YAMLKeyValue keyValue = null;
        for (YAMLPsiElement yamlElement : yamlFile.getYAMLElements()) {
            YAMLDocument yamlDocument = (YAMLDocument) yamlElement;

            // 找映射
            YAMLMapping yamlMapping = null;
            for (YAMLPsiElement psiElement : yamlDocument.getYAMLElements()) {
                if (psiElement instanceof YAMLMapping tempMapping) {
                    yamlMapping = tempMapping;
                    break;
                }
            }
            if (yamlMapping == null) {
                continue;
            }

            // 找methodName:...
            for (YAMLKeyValue tempKeyValue : yamlMapping.getKeyValues()) {
                if (tempKeyValue.getValue() != null && tempKeyValue.getValue().getText().equals(psiMethod.getName())) {
                    keyValue = tempKeyValue;
                    break;
                }
            }
            if (keyValue != null) {
                break;
            }
        }
        if (keyValue == null) {
            return;
        }

        NavigationGutterIconBuilder<PsiElement> methodBuilder = NavigationGutterIconBuilder.create(NavigationIcon.NAV_ICON)
            .setAlignment(GutterIconRenderer.Alignment.CENTER)
            .setTarget(keyValue)
            .setTooltipTitle("Navigation to Target In Mapper Yaml");
        PsiIdentifier nameIdentifier = psiMethod.getNameIdentifier();
        if (nameIdentifier != null) {
            result.add(methodBuilder.createLineMarkerInfo(nameIdentifier));
        }
    }
}
